package PagePackage;

import static org.testng.AssertJUnit.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import PagePackage.teamsPage;


public class newTeamPage extends internalPageClass{
	@FindBy (xpath="html/body/div/main/section/aside/nav/ul/li[1]/a")
	public WebElement teamMemberTab;	
	
	
	@FindBy (name="sport")
	public WebElement sportDropdown;	
	
	
	@FindBy (name="league")
	public WebElement leagueDropdown;	
	
	@FindBy (name="school")
	public WebElement schoolDropdown;
	
	@FindBy (name="name")
	public WebElement teamNameField;
	
	
	@FindBy (xpath="html/body/div/main/section/main/button")
	public WebElement saveTeamButton;
	
	@FindBy (xpath="html/body/div/main/section/main/a")
	public WebElement cancelTeamButton;
	
	public newTeamPage chooseTeamSport(final String teamSport) {
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String sportDropdown = driver.findElement(By.name("sport")).getAttribute("name");
		            		Select select3 = new Select(driver.findElement(By.name(sportDropdown)));
		            		 select3.selectByVisibleText(teamSport);
		                	
		                  
		                }
		                catch( StaleElementReferenceException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "Sport found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		return MyPageFactory.getPage(driver, newTeamPage.class);
	/*Select select7 = new Select(sportDropdown);
	 select7.selectByVisibleText(teamSport);
	 return MyPageFactory.getPage(driver, newTeamPage.class);*/

	}
	
	
	public newTeamPage chooseLeagueSport(final String leagueName){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String leagueDropdown = driver.findElement(By.name("league")).getAttribute("name");
		            		Select select3 = new Select(driver.findElement(By.name(leagueDropdown)));
		            		 select3.selectByVisibleText(leagueName);
		                	
		                  
		                }
		                catch( StaleElementReferenceException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "League found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
	/*Select select8 = new Select(leagueDropdown);
	 select8.selectByVisibleText(leagueName);*/
	 return MyPageFactory.getPage(driver, newTeamPage.class);
	}
	 
	public newTeamPage chooseSchoolSport(final String schoolName){
		/*Select select8 = new Select(schoolDropdown);
		 select8.selectByVisibleText(schoolName);*/
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String schoolDropdown = driver.findElement(By.name("school")).getAttribute("name");
		            		Select select3 = new Select(driver.findElement(By.name(schoolDropdown)));
		            		 select3.selectByVisibleText(schoolName);
		                	
		                  
		                }
		                catch( StaleElementReferenceException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "School found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		 return MyPageFactory.getPage(driver, newTeamPage.class);
		}
	 
	public newTeamPage addTeamName (String teamName){
		teamNameField.clear();
		teamNameField.sendKeys(teamName);
		 return MyPageFactory.getPage(driver, newTeamPage.class);
		}
	
	
	public teamsPage saveTeam (){
		saveTeamButton.click();
		 return MyPageFactory.getPage(driver, teamsPage.class);
		}
	
	public teamsPage cancelTeam (){
		cancelTeamButton.click();
		 return MyPageFactory.getPage(driver, teamsPage.class);
		}
	public newTeamPage checkLeague(final String league){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String selectedOption = new Select(driver.findElement(By.name("league"))).getFirstSelectedOption().getText();

		                	assertEquals(league, selectedOption);
		                	
		                  
		                }
		                catch( AssertionError e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "League found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		 return MyPageFactory.getPage(driver, newTeamPage.class);}
	
	
public newTeamPage checkSport(final String sport){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String selectedOption = new Select(driver.findElement(By.name("sport"))).getFirstSelectedOption().getText();

		                	assertEquals(sport, selectedOption);
		                	
		                  
		                }
		                catch( AssertionError e )
		                {
		                	System.out.println( "Sport doesn't match" );
		                    return false;
		                }
		                System.out.println( "Sport found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		 return MyPageFactory.getPage(driver, newTeamPage.class);}



public newTeamPage checkSchool(final String school){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String selectedOption = new Select(driver.findElement(By.name("school"))).getFirstSelectedOption().getText();

		                	assertEquals(school, selectedOption);
		                	
		                  
		                }
		                catch( AssertionError e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "League found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		 return MyPageFactory.getPage(driver, newTeamPage.class);}
	 
	public boolean isOnThisPage(){
		return teamMemberTab.isDisplayed();
	}
 @Override
void tryToOpen() {
	 goToInternalPage2()
	   .logout()
	   .loginAs("victoria@krossover.com", "webmaster")
	   .clickteamtab();
	
}

}
